package com.goods.controller.business;

import com.goods.business.service.ProductService;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "物资资料")
@RestController
@RequestMapping("/business/product")
public class ProductController {
    @Resource
    private ProductService productService;

    //查询
    @GetMapping("/findProductList")
    public ResponseBean findProductList(Integer pageNum, Integer pageSize, ProductVO productVo, String categorys, String supplier) {
        List<ProductVO> list = productService.findProductList(productVo, categorys);
        List<ProductVO> page = ListPageUtils.page(list, pageSize, pageNum);
        Map map = new HashMap();
        map.put("rows", page);
        map.put("total", list.size());
        return ResponseBean.success(map);
    }

    //添加
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductVO productVO) {
        Integer integer = productService.add(productVO);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("添加失败惹");
        }
    }

    //回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ProductVO productVO = productService.edit(id);
        if (productVO != null) {
            return ResponseBean.success(productVO);
        } else {
            return ResponseBean.error("数据不见惹");
        }
    }

    //修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody ProductVO productVO) {
        Integer integer = productService.update(id, productVO);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("修改失败惹");
        }

    }

    //移入回收站
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        Integer integer = productService.remove(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("移入回收站失败惹");
        }
    }

    //从回收站恢复
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id) {
        Integer integer = productService.back(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("恢复失败惹");
        }
    }

    //永久移除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        Integer integer = productService.delete(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("删除失败惹");
        }
    }

    //审核
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id) {
        Integer integer = productService.publish(id);
        if (integer > 0) {
            return ResponseBean.success();
        } else {
            return ResponseBean.error("删除失败惹");
        }
    }

    //查找正常状态的商品
    @GetMapping("/findProducts")
    private ResponseBean findProducts(Integer pageSize, Integer pageNum, ProductVO productVO,String categorys) {
        List<ProductVO> list = productService.findProductList(productVO,categorys);
        List<ProductVO> page = ListPageUtils.page(list, pageSize, pageNum);
        return ResponseBean.success(new PageVO<>(list.size(),page));
    }
    //查找正常状态的商品和数量   柱状图
    @GetMapping("/findProductStocks")
    private ResponseBean findProductStocks(Integer pageSize, Integer pageNum, ProductVO productVO,String categorys) {
        List<ProductStockVO> list = productService.findProductStocks(productVO,categorys);
        List<ProductStockVO> page = ListPageUtils.page(list, pageSize, pageNum);
        return ResponseBean.success(new PageVO<>(list.size(),page));
    }
    //查找正常状态的商品和数量      饼状图
    @GetMapping("/findAllStocks")
    private ResponseBean findAllStocks(Integer pageSize, Integer pageNum, ProductVO productVO,String categorys) {
        List<ProductStockVO> list = productService.findProductStocks(productVO,categorys);
        List<ProductStockVO> page = ListPageUtils.page(list, pageSize, pageNum);
        return ResponseBean.success(page);
    }
}
